<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../../css/common.css" media="all">
		<style type="text/css">
			.layui-table th {
				position: relative;
				min-height: 20px;
				height: 35px;
				line-height: 20px;
				font-size: 14px;
				padding: 0px 0px;
			}
			
			.layui-table td {
				position: relative;
				min-height: 20px;
				height: 100%;
				line-height: 14px;
				font-size: 14px;
				padding: 0px 0px;
			}
			
			.layui-input,
			.layui-textarea {
				display: block;
				width: 100%;
				padding: 0px 0px;
				border: 0px;
			}
			
			.layui-input{
				text-align: center;
			}
		</style>
	</head>

	<body>

		<div class="layui-fluid">
			<div class="layui-row" id="view">
				<form class="layui-form" lay-filter="test_detail">
					<div class="layui-card">
						<div class="layui-form-item">
							<label class="layui-form-label"><!-- 活动时间 --><!-- <span style="color:red">*</span> --></label>
							<div class="layui-input-inline" style="display: flex;align-items: center;height: 2.375rem;padding: 0.5625rem 0;">
								<!-- <input type="text" name="dates" lay-verify="required" disabled placeholder="请选择" autocomplete="on" class="layui-input limited_name"> -->
								<span id="name"></span>
								<!-- <input type="hidden" name="seckill_id" lay-verify="required" placeholder="" autocomplete="off" class="layui-input seckill_id"> -->
							</div>
							<div class="layui-form-mid layui-word-aux">
								<!-- <select id="select" lay-filter="level">
								  <option id="selectOp" value="">请选择一个佣金模板</option>
								  <option value="1">模板1</option>
								</select> -->
								<!-- <button type="button" class="layui-btn layui-btn-sm" onclick="opentemp()">选择模板</button> -->
								<!-- <a href="Javascript:;" onclick="searchdata('market_id')">选择</a> -->
							</div>
						</div>
						<div class="layui-card-body">
							<table class="layui-table" id="testtable">
								<!-- <table class="layui-table" lay-size="sm"> -->
								<colgroup>
									<col>
									<col>
									<col>
									<col>
									<col>
								</colgroup>
					
								<thead>
									<tr>
										<!-- <th></th> -->
										<th style="width: 100px;">商品编号</th>
										<!-- <th style="width: 250px;">商品名</th> -->
										<!-- <th style="width: 70px;">会员价</th> -->
										<!-- <th>原抵扣积分zhu</th> -->
										<th style="width: 70px;">团购价</th>
										<!-- <th>积分比例%zhu</th> -->
										<!-- <th>积分使用zhu</th> -->
										<th style="width: 70px;">补贴</th>
										<th style="width: 70px;">团人数</th>
										<th style="width: 70px;">中奖人数</th>
										<th style="width: 120px;">显示已参团人数</th>
										
										<th style="width: 70px;">活动数量</th>
										<th style="width: 120px;">显示已购数量</th>
										<th style="width: 70px;">显示数量</th>
										<th style="width: 120px;">单人购买上限</th>
										
										<th style="width: 60px;">活动时长</th>
										<th style="width: 90px;">发起奖金</th>
										<th style="width: 90px;">直推佣金</th>
										<th style="width: 90px;">间推佣金</th>
										<th style="width: 90px;">团长佣金</th>
										<th style="width: 90px;">管理奖</th>
										<th style="width: 100px;">合伙人佣金</th>
										<th style="width: 40px;">排序</th>
										<th style="width: 90px;">提交状态</th>
									</tr>
								</thead>
					
								<tbody id="temp">
									
								</tbody>
							</table>
						</div>
					</div>
					<div class="layui-form-item layui-layout-admin">
						<div class="layui-input-block">
							<div class="layui-footer" style="left: 0;">
								<button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
		<script src="../../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../../js/jquery.min.js"></script>
		<script src="../../../../js/jquery.jqprint-0.3.js"></script>
		<script src="../../../../js/common.js"></script>
		<script>
			var _data = {
				id: getParam('id')
			}
			function child(skuid){
				console.log(skuid, 'skuid')
				$("textarea[name=skuids]").val(skuid)
				_data.id = skuid
			}
			layui.config({
				base: '../../../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'form', 'table'], function() {
				var $ = layui.$,
					admin = layui.admin,
					form = layui.form;
					table = layui.table;
					
					
				if(_data.id !== null) {
					let list = _data.id.split(',')
					let promises = [];
					list.forEach(item => {
						loading();
						let promise = initdata(item)
						promises.push(promise)
					})
					Promise.all(promises).then((res) => {
						closeloading();
						window.list = res
						tableDate()
					})
				}
				
				//监听提交
				form.on('submit(demo1)', function(data) {
					var data = data.field;
					var testtable = document.getElementById('testtable');
					var rows = testtable.rows;
					layer.confirm('确定要提交数据', async function() {
						loading();
						let promisese = [];
						for(let i=0;i<rows.length;i++) {
							var obj = {};
							var cells = rows[i].cells;
							var skuid = skuid = $(cells).find('input[name=skuid]').val(),
								price = $(cells).find('input[name=price]').val(),//团购价格
								// stock = $(cells).find('input[name=stock]').val(),//库存数量
								sales = $(cells).find('input[name=sales]').val(), //已购买数量
								num = $(cells).find('input[name=num]').val(), //显示数量
								top_count = $(cells).find('input[name=top_count]').val(), //购买上限
								subsidy = $(cells).find('input[name=subsidy]').val(), //补贴
								group_num = $(cells).find('input[name=group_num]').val(), //团人数
								group_wnum = $(cells).find('input[name=group_wnum]').val(), //中奖人数
								group_onum = $(cells).find('input[name=group_onum]').val(), //显示团参与人数
								id = $(cells).find('input[name=id]').val(),
								duration = $(cells).find('input[name=duration]').val(),
								launch_fee = $(cells).find('input[name=launch_fee]').val(),
								leader_fee = $(cells).find('input[name=leader_fee]').val(), // 团长佣金
								direct_fee = $(cells).find('input[name=direct_fee]').val(), // 直推奖励
								indirect_fee = $(cells).find('input[name=indirect_fee]').val(),/* 间推奖励 */
								team_fee = $(cells).find('input[name=team_fee]').val(), // 团队奖励 === 管理奖
								copartner_fee = $(cells).find('input[name=copartner_fee]').val(); // 合伙人奖励
								
								if(skuid) {
									obj = {
										id,
										skuid,
										price,
										sales,
										num,
										top_count,
										subsidy,
										group_num,
										group_wnum,
										group_onum,
										duration,
										launch_fee,
										leader_fee,
										direct_fee,
										indirect_fee,
										team_fee,
										copartner_fee
									}
									// let promise = submitdata2(server.seckill_goods_edit, obj)
									// promisese.push(promise)
									const res = await submitdata2(server.seckill_goods_edit, obj)
									if (res) {
										let text = '已提交'
										$(cells).find("input[name=submit]").val(text)
										closeloading();
										layer.alert('操作成功', function(){
											parent.layer.closeAll()
											parent.layui.table.reload('test-table-operate')
										})
									} else {
										layer.msg('服务器连接错误')
									}
								}
						}
						// Promise.all(promisese).then((res) => {
						// 	// console.log(window.list, 'kkkkkkk')
						// 	console.log('循环结束', res)
						// 	res.forEach((item, index) => {
						// 		if(item){
						// 			let cells = rows[index].cells;
						// 			$(cells).find("input[name=submit]").val("已提交")
						// 		}
						// 	})
						// })
					}, function(){
						layer.msg('取消操作')
					})
					return false
					// _data = Object.assign(_data, data);
					// console.log(data, 'data---tijiao')
					// layer.confirm('确定要提交数据？', function() {
					// 	loading();
					// 	submitdata(server.seckill_goods_edit);
						 
					// }, function() {
					// 	layer.msg('取消操作');
					// });
					// return false;
				});
				
				
				function submitdata2(_url, data) {
					return new Promise((resolve, reject)=>{
						$.post(_url, data, function(res, status) {
							if(status == 'success') {
								if (res.code == 0) {
									// layer.msg('操作成功')
									resolve(true)
								} else {
									closeloading();
									reject('服务器连接错误')
								}
							} else {
								closeloading()
								reject('服务器连接错误')
							}
						})
					})
				}
				
				
				// function submitdata(_url) {
				// 	$.post(_url, _data, function(res, status) {
				// 		if(status == 'success') {
				// 			if(res.code == 0) {
				// 				layer.alert('操作成功', function() {
				// 					parent.layer.closeAll();
				// 					parent.layui.table.reload('test-table-operate');
				// 				});
				// 			} else {
				// 				layer.msg(res.msg);
				// 			}
				// 			closeloading();
				// 		} else {
				// 			closeloading();
				// 			layer.msg('服务器连接错误');
				// 		}
				// 	});
				// }

				window.searchdata = function(type) {
					switch(type) {
						case 'skuid':
							layer.open({
								type: 2,
								title: '选择',
								shadeClose: false,
								shade: 0.5,
								maxmin: true, //开启最大化最小化按钮
								area: ['90%', '98%'],
								content: 'choose_skuid.html',
								end: function() {},
								success: function() {}
							});
							break;
						case 'seckill_id':
							layer.open({
								type: 2,
								title: '选择',
								shadeClose: false,
								shade: 0.5,
								maxmin: true, //开启最大化最小化按钮
								area: ['90%', '90%'],
								content: 'choose_limited.html',
								end: function() {},
								success: function() {}
							});
							break;
					}
				}
				window.list = []
				function initdata(id) {
					return new Promise((resolve, reject)=>{
						$.get(server.seckill_goods_detail, { id }, function(res, status) {
							if(status == 'success') {
								if(res.code == 0) {
									var data = res.seckill_goods.data
									resolve(data)
									// window.list.push(data)
									// form.val("test_detail", data.data);
									// form.render();
								} else {
									layer.msg(res.msg);
								}
							} else {
								layer.msg('服务器连接错误');
							}
						});
					})
					// $.get(server.seckill_goods_detail, { id }, function(res, status) {
					// 	if(status == 'success') {
					// 		if(res.code == 0) {
					// 			var data = res.seckill_goods.data
					// 			window.list.push(data)
					// 			// form.val("test_detail", data.data);
					// 			// form.render();
					// 		} else {
					// 			layer.msg(res.msg);
					// 		}
					// 	} else {
					// 		layer.msg('服务器连接错误');
					// 	}
					// });
				}
				
				// setTimeout(()=>{
				// 	// console.log(window.list, '000')
				// 	tableDate()
				// },1000)
				
				function tableDate() {
					var html = ''
					for(let i=0;i<window.list.length;i++) {
						html += `<tr>` +
						`<td><div class="flex"><input  class="layui-input" required lay-verify="required" disabled  name="skuid" value="${window.list[i].skuid}" /><input type="hidden" class="layui-input" required lay-verify="required" name="id" value="${window.list[i].id}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" type="text" required lay-verify="required" name="price" value='${window.list[i].price}' /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="subsidy" value="${window.list[i].subsidy}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="group_num" value="${window.list[i].group_num}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="group_wnum" value="${window.list[i].group_wnum}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="group_onum" value="${window.list[i].group_onum}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="stock" value="${window.list[i].stock}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="sales" value="${window.list[i].sales}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="num" value="${window.list[i].num || ''}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="top_count" value="${window.list[i].top_count}" /></div></td>`+
						`<td><div clas="flex"><input class="layui-input" required lay-verify="required" name="duration" value="${window.list[i].duration}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="launch_fee" value="${window.list[i].launch_fee}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="direct_fee" value="${window.list[i].direct_fee}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="indirect_fee" value="${window.list[i].indirect_fee}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="leader_fee" value="${window.list[i].leader_fee}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="team_fee" value="${window.list[i].team_fee}" /></div></td>`+
						`<td><div class="flex"><input class="layui-input" required lay-verify="required" name="copartner_fee" value="${window.list[i].copartner_fee}" /></div></td>`+
						`<td><div class="flex">${i+1}</div></td>`+
						`<td><div class="flex"><input class="layui-input" disabled  name="submit" value="未提交" /></td>`+
						`</tr>`;
					}
					$('#temp').before(html)
				}
				
				
				
				
				// $("input[name=score]").bind("blur",function(e){
				// 	var market_price = $("input[name=price]").val()
				// 	var score = $("input[name=score]").val()
				// 	console.log(market_price)
				// 	console.log(score)
				// 	if (score> 0 &&  market_price>0) {
				// 		var sumScale = Math.round(parseFloat((score/market_price).toFixed(2)) * 100)  
				// 		$("input[name=score_scale]").val(sumScale)
				// 	}
				// })
				// $("input[name=score_scale]").bind("blur",function(e){
				// 	var market_price = $("input[name=price]").val()
				// 	var score_scale = $("input[name=score_scale]").val()
				// 	if (score_scale >0 &&  score_scale <=100 && market_price>0 ) {
				// 		var sumScore = Math.round(parseFloat((market_price*score_scale).toFixed(2)) * 100) /10000
				// 		$("input[name=score]").val(sumScore)
				// 	}
				// })
			});
		</script>
	</body>

</html>